//
//  HelloWorldViewController.m
//  HelloWorld
//
//  Created by Haiyang Xu on 6/7/12.
//  Copyright (c) 2012 SJTU. All rights reserved.
//

#import "HelloWorldViewController.h"

@interface HelloWorldViewController ()

@end

@implementation HelloWorldViewController
@synthesize textField;
@synthesize label;
@synthesize timeLabel;
@synthesize userName = _userName;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setTextField:nil];
    [self setLabel:nil];
    [self setTimeLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)changeGreeting:(id)sender {
    self.userName = self.textField.text;
    NSString *nameString = self.userName;
    if([nameString length] == 0){
        nameString = @"World";
    }
    
    NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
    self.label.text = greeting;
    
    NSDateComponents *dc = [self getCurrentDate];
    
    //test for subversion
    NSString *time = [[NSString alloc] initWithFormat:@"The time is %d:%d:%d", [dc hour], [dc minute], [dc second]];
    self.timeLabel.text = time;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *) theTextField{
    if(theTextField == self.textField){
        [theTextField resignFirstResponder];
    }
    return YES;
}

- (NSDateComponents *) getCurrentDate{
    NSDate *now = [NSDate date]; // 1
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 2
    
    [calendar setTimeZone:[NSTimeZone systemTimeZone]]; // 3
    NSDateComponents *dc = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:now];  // 4
    
    NSString *teststring = [[NSString alloc] initWithFormat:@"halala"];
    
    return dc;
}


@end
